//
//  HMEmoticonPageCell.swift
//  EmoticonKeyboard
//
//  Created by 周英培 on 16/5/30.
//  Copyright © 2016年 周英培. All rights reserved.
//  表情视图collectionView的cell

import UIKit
import SnapKit
class HMEmoticonPageCell: UICollectionViewCell {
    
    //给cell传递模型数据
    var emoticons : [HMEmoticon]? {
    
        didSet {
        
//            //给每个按钮赋值数据
//            for (index,btn) in  buttons.enumerate(){
//                
//                //获取模型
//                let emoticon = emoticons![index]
//                
//                //这里直接加载图片名称是不行的 因为图片不在Assets.xcassets里面 所以需要通过路径去加载
//                btn.setImage(UIImage(named:emoticon.imagePath! ), forState: .Normal)
//                
//            }
            //给每个按钮赋值数据 不能遍历按钮 因为按钮每一页都是按照21个去计算的 最有一页会崩溃
            for (index,emoticon) in  emoticons!.enumerate(){
                
                //获取模型
                let btn = buttons[index]
                
//                //这里直接加载图片名称是不行的 因为图片不在Assets.xcassets里面 所以需要通过路径去加载
//                btn.setImage(UIImage(named:emoticon.imagePath ?? "" ), forState: .Normal)
//                btn.setTitle(emoticon.emoji, forState: .Normal)
                
                btn.emoticon = emoticon
                
            }

        
        }
    
    }
    
    //定义按钮数组 存储按钮
//    private lazy var buttons : [UIButton] = [UIButton]()
    private lazy var buttons : [HMEmoticonButton] = [HMEmoticonButton]()
    
    
//    //定义属性
//    var indexPath: NSIndexPath? {
//    
//        didSet {
//        
//        label.text = "第\(indexPath?.section ?? 0)组,第\(indexPath?.item ?? 0)cell"
//        
//        }
//    
//    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:设置UI
    private func setUpUI() {
        contentView.addSubview(label)
        setEmoticon()
        
        //设置约束
        label.snp_makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
        
        //添加手势  添加在contentview上面
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressFire(_:)))
        self.contentView.addGestureRecognizer(longPress)
      
    }
    
   @objc private func longPressFire(ges: UILongPressGestureRecognizer) {
    
        //触摸点
        let point = ges.locationInView(contentView)
    
    //根据触摸点 获取对应的按钮
    guard let btn = buttonWithPoint(point) else {
    
        popview.disMissAfter()
        
        return
    
    }
    
    //如果手势在删除或者空白上面不添加popview
    if btn.emoticon!.isDelete || btn.emoticon!.isSpace {
        popview.disMissAfter()

        return
    }
    
    //根据状态来设置popview
    switch ges.state {
    case .Began,.Changed:
        
        popview.show(btn)
       
    default:
        popview.disMissAfter()
    }
    
    
    }
    
   
    
    //懒加载popview
    private lazy var popview : HMEmoticonPopView = {
    
        let pop = HMEmoticonPopView.loadPopView()
        
        return pop
    
    }()
    
    //根据手势的触摸点  获取对应的按钮
    private func buttonWithPoint(point:CGPoint) -> HMEmoticonButton? {
    
        for btn in buttons {
            
            //根据按钮的frame是否包含触摸点 来获取对应的按钮
            if btn.frame.contains(point) {
                return btn
            }
  
        }
        
        return nil
    
    }
    
    //每页上面加载21个表情
    private func setEmoticon() {
    
        let margin : CGFloat = 5
        let bottomMargin : CGFloat = 30
        
        let btnW : CGFloat = (UIScreen.mainScreen().bounds.width - 2 * margin) / CGFloat(EmoticonColCount)
        let BtnH : CGFloat = (self.bounds.height - bottomMargin) / CGFloat(EmoticonRowCount)
        
        for i in 0..<21 {
            let btn = HMEmoticonButton()
            
//            btn.setTitle("周杰伦", forState: .Normal)
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            let row  = i / EmoticonColCount
            let col  = i % EmoticonColCount
            
            let btnX = margin + CGFloat(col) * btnW
            let btnY = CGFloat(row) * BtnH
         
            btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: BtnH)
            
            //给按钮添加点击事件
            btn.addTarget(self, action: #selector(btnClick(_:)), forControlEvents: .TouchUpInside)
            
            contentView.addSubview(btn)
            
            buttons.append(btn)
        }
    
    }
    
    @objc private func btnClick(btn:HMEmoticonButton) {
    
        //在这里发送通知，让textview显示图片 图片包含在模型中
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonBtnDidClickNotification, object: btn.emoticon)
    
    }
    //MARK:懒加载
    private lazy var label : UILabel = {
    
        let l = UILabel()
        
        l.font = UIFont.systemFontOfSize(14)
        
        l.sizeToFit()
        
        return l
    
    }()
}
